const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ios:app.globalData.ios,//是否为ios
    id:'',//商品id
    show:false,//弹窗是否出来
    current:0,
    content:'',//富文本内用
    address:'',//默认地址
    // domain:Api.domain,
    jz:'',//换算率
    jf:'',//我的积分
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      jz:wx.$cache.get('can').integral_price_proportion,
    })
    console.log(this.data.jf,this.data.jz,'ddddddddddddddd')
    this.init()
  },
  init(){//商品详情
    var that = this
    var params = {
      goods_id:this.data.id
    }
    wx.$http.get(wx.$get.goods_info,params).then(res=>{
      console.log('商品详情', res)
      that.setData({
        items:res,
        content:res.content
      })
    })
  },
  bindqei(e){//录播图切换
    this.setData({
      current:e.detail.current
    })
  },
  moren(){//默认地址
    var that = this
    wx.$http.get(wx.$get.default_goods_address,{}).then(res=>{
      console.log(res,'默认地址')
      that.setData({
        address:res
      })
    })
  },
  goaddress(){//去填写收货地址
    wx.navigateTo({
      url: '/pages/shippingAddress/shippingAddress',
    })
  },
  gopay(){//去确认订单页面
    if(this.data.ios){
      if(this.data.jf>=this.data.items.integral_t){
        wx.navigateTo({
          url: `/pages/confirmOrder/confirmOrder?id=${this.data.id}`,
        })
      }else{
        wx.showToast({
          title: '抱歉，积分不够',
          icon: 'none',
          duration: 2000
        })
      }
    }else{
      wx.navigateTo({
        url: `/pages/confirmOrder/confirmOrder?id=${this.data.id}`,
      })
    }
  },
  goshow(){//兑换码弹窗出来
    if(this.data.items.type==1&&this.data.address){
      this.setData({
        show:true
      })
    }else if(this.data.items.type!=1){
      this.setData({
        show:true
      })
    }else{
      wx.navigateTo({
        url: '/pages/shippingAddress/shippingAddress',
      })
    }
  },
  zhi(e){//立即支付
    console.log(e)
    var that = this
    let params = {
      goods_id:this.data.id,
      pay_type:2,
      province:this.data.address.province?this.data.address.province:'',
      city:this.data.address.city?this.data.address.city:'',
      area:this.data.address.area?this.data.address.area:'',
      address:this.data.address.address?this.data.address.address:'',
      mobile:this.data.address.mobile?this.data.address.mobile:'',
      realname:this.data.address.realname?this.data.address.realname:'',
      activation_code:e.detail
    }
    wx.$http.get(wx.$get.add_goods_order,params).then(res=>{
      console.log('立即支付', res)
      that.setData({
        show:false
      })
      wx.redirectTo({
        url:`/pages/myOrder/myOrder?type=${that.data.items.type}`
      })
    })
  },
  goshuo(){//领取说明
    if(this.data.items.no_money_type==1){
      app.globalData.content=this.data.items.no_money_intro
    }else{
      app.globalData.content=this.data.items.no_money_url
    }
    wx.navigateTo({
      url: `/pages/waparse/waparse?is_url=${this.data.items.no_money_type}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.$cache.get('dz')){
      this.setData({
        address:wx.$cache.get('dz')
      })
    }
    this.setData({
      jf:wx.$cache.get('userinfo').integral
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})